package com.java.app.hacktouch.gesture

import android.util.Log
import com.java.app.hacktouch.bluetooth.BtConstant
import com.java.app.hacktouch.bluetooth.BtIf
import java.util.*

/**
 * Created by guanguojin on 2017/11/13.
 */

object OperationGesture {

    private val TAG = "OperationGesture"

    fun excuteGesture(name: String) {

        val multi_key_array = name.split("\\+".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        var multi_key_num = 0
        val multi_key_val = ArrayList<Int>()

        multi_key_val.add(0) //the first value is used for special value
        multi_key_num++

        for (key in multi_key_array) {
            if (GestureBitmap.isSpecial(key)) {
                multi_key_val[0] = multi_key_val[0] or GestureBitmap.getValue(key)
            } else {
                multi_key_val.add(GestureBitmap.getValue(key))
                multi_key_num++
            }
        }

        val mdata = IntArray(1 + 1 + 1 + multi_key_num)
        mdata[0] = BtConstant.BE_SOF
        mdata[1] = multi_key_num
        mdata[2] = BtConstant.BE_TYPE_GESTURE
        for (idx in 0 until multi_key_num) {
            mdata[3 + idx] = multi_key_val[idx]
        }

        BtIf.mCommandService!!.write(mdata)
        try {
            Thread.sleep(500)
        } catch (e: InterruptedException) {
            Log.e(TAG, "InterruptedException", e)
        }

        for (idx in 0 until multi_key_num) {
            mdata[3 + idx] = 0
        }
        BtIf.mCommandService!!.write(mdata)

    }
}
